package index;

import Common.GoggleDB;
import Transaction.Transaction;
import storage.data.Page;
import storage.data.PageID;
import heap.HFPage;


public class BTHeaderPage extends HFPage {

	void setPageID(PageID pageno,Transaction tx,String table) throws Exception {
		setCurPage(pageno,tx,table);
	}

	PageID getPageID(Transaction tx,String table) throws Exception {
		return getCurPage(tx,table);
	}

	void setMagic0(int magic,Transaction tx,String table) throws Exception {
		setPrevPage(new PageID(magic),tx,table);
	}
	int getMagic0(Transaction tx,String table) throws Exception {
		return getPrevPage(tx,table).pageId;
	};

	void setRootId(PageID rootID,Transaction tx,String table) throws Exception {
		setNextPage(rootID,tx,table);
	};

	PageID getRootId(Transaction tx,String table) throws Exception {
		return getNextPage(tx,table);
	}

	/**
	 * set the key type
	 */
	void setKeyType(short key_type,Transaction tx,String table) throws Exception {
		setSlot(3, (int) key_type, 0,tx,table,curPage);
	}

	/**
	 * get the key type
	 */
	public short getKeyType(Transaction tx,String table) throws Exception {
		return (short) getSlotLength(3,tx,table);
	}

	/**
	 * get the max keysize
	 */
	void setMaxKeySize(int key_size,Transaction tx,String table) throws Exception {
		setSlot(1, key_size, 0,tx,table,curPage);
	}

	/**
	 * set the max keysize
	 */
	int getMaxKeySize(Transaction tx,String table) throws Exception {
		return getSlotLength(1,tx,table);
	}

	/**
	 * set the delete fashion
	 */
	void setDeleteFashion(int fashion,Transaction tx,String table) throws Exception {
		setSlot(2, fashion, 0,tx,table,curPage);
	}

	/**
	 * get the delete fashion
	 */
	int getDeleteFashion(Transaction tx,String table) throws Exception {
		return getSlotLength(2,tx,table);
	}

	/**
	 * pin the page with pageno, and get the corresponding SortedPage
	 */
	public BTHeaderPage(PageID pageno) throws Exception {
		super();
		GoggleDB.bufferMgr.pinPage(pageno, this, false/* Rdisk */);
	}

	/** associate the SortedPage instance with the Page instance */
	public BTHeaderPage(Page page) {
		super(page);
	}

	/**
	 * new a page, and associate the SortedPage instance with the Page instance
	 */
	public BTHeaderPage(String filename,Transaction tx) throws Exception {
		super();
		Page apage = new Page();
		PageID pageId = GoggleDB.storageInterface.add_db(tx,filename);
		GoggleDB.bufferMgr.pinPage(pageId, apage, true);
		this.init(pageId, apage,tx,filename);

	}
}
